package com.servlet.project.service;

import com.servlet.project.entity.Member;
import com.servlet.project.util.JdbcUtil;

import java.util.List;

public class MemberService {

    /**
     * 会员注册
     * @param obj
     * @return
     */
    public int add(List<Object> obj) {
        String sql="insert into member(membername,memberpassword,memberbirthdate,membersex,memberemail) values(?,?,?,?,?) ";
        int count= JdbcUtil.executeUpdate(sql, obj);
        return count;
    }

    /**
     * 会员登录
     * @param obj
     * @return
     */
    public Member login(List<Object> obj) {
        String sql="select * from member where membername=? and memberpassword=?";
        List<Member> members= JdbcUtil.executeQuery(sql, obj,Member.class);
        if(members!=null&&members.size()>=1){
            return members.get(0);
        }
        return null;
    }


    public List<Member> getListAll(int page, int rows, Member obj) {
        String sql="select * from member where 1=1 ";
        if(obj.getMembername()!=null&&!obj.getMembername().equals("")){
            sql+=" and membername like '%"+obj.getMembername()+"%' ";
        }
        sql+=" limit "+page+","+rows;
        List<Member> memberList= JdbcUtil.executeQuery(sql,null,Member.class);
        return memberList;
    }

    public int getListCount(Member obj) {
        String sql="select * from member where 1=1 ";
        if(obj.getMembername()!=null&&!obj.getMembername().equals("")){
            sql+=" and membername like '%"+obj.getMembername()+"%' ";
        }
        List<Member> memberList= JdbcUtil.executeQuery(sql,null,Member.class);
        return memberList.size();
    }


    public int up(List<Object> objects) {
        String sql="update member set membername=?,memberpassword=?,memberbirthdate=?,membersex=?,memberemail=? where memberid=? ";
        int count= JdbcUtil.executeUpdate(sql, objects);
        return count;
    }

    public Member getId(String memberId) {
        String sql="select * from member where memberid="+memberId;
        List<Member> users= JdbcUtil.executeQuery(sql,null,Member.class);
        return users.get(0);
    }

    public int del(List<Object> objects) {
        String sql="delete from member  where memberid=? ";
        int count=JdbcUtil.executeUpdate(sql, objects);
        return count;
    }


}
